package com.ittony.controller.admin;

import com.ittony.constant.JwtClaimsConstant;
import com.ittony.dto.EmployeeDTO;
import com.ittony.dto.EmployeeLoginDTO;
import com.ittony.dto.EmployeePageQueryDTO;
import com.ittony.entity.Employee;
import com.ittony.properties.JwtProperties;
import com.ittony.result.PageResult;
import com.ittony.result.Result;
import com.ittony.service.EmployeeService;
import com.ittony.utils.JwtUtil;
import com.ittony.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "Employee Interface")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private JwtProperties jwtProperties;

    @PostMapping("/login")
    @ApiOperation(value = "Employee Login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("Employee Login:{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        HashMap<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(jwtProperties.getAdminSecretKey(), jwtProperties.getAdminTtl(), claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .username(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);


    }

    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

    @PostMapping
    @ApiOperation("Add New Employee")
    public Result save(@RequestBody EmployeeDTO employeeDTO) {
        log.info("Add New Employee:{}", employeeDTO);
        employeeService.save(employeeDTO);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("Employee Page")
    public Result<PageResult> page(EmployeePageQueryDTO employeePageQueryDTO) {
        log.info("Employee Page:{}", employeePageQueryDTO);
        PageResult pageResult = employeeService.pageQuery(employeePageQueryDTO);
        return Result.success(pageResult);
    }

    @PostMapping("/status/{status}")
    @ApiOperation("Enable/Disable Employee Account")
    public Result startOrStop(@PathVariable Integer status,Long id) {
        log.info("Enable/Disable Employee Account:{}", status,id);
        employeeService.startOrStop(status,id);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("Find Employee Info based on Id")
    public Result findEmployeeById(@PathVariable Long id) {
        log.info("Find Employee Info based on Id:{}", id);
        Employee employee = employeeService.findEmployeeById(id);
        return Result.success(employee);
    }

    @PutMapping
    @ApiOperation("Edit Employee Info")
    public Result update(@RequestBody EmployeeDTO employeeDTO) {
        log.info("Update Employee Info:{}", employeeDTO);
        employeeService.update(employeeDTO);
        return Result.success();
    }

}
